package com.un.ebs.sys.controller;


import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.dto.UserAddDto;
import com.un.ebs.sys.dto.UserDto;
import com.un.ebs.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/user")
@Api(value = "sys-user", description = "用户")
public class UserController {

    @Autowired
    private UserService service;

    @PostMapping("login")
    @ApiOperation(value = "登录")
    public WebResult login(@RequestParam("username") String username, @RequestParam("password") String password) {
        return WebUtils.success(service.login(username, password, null, null, null));
    }

    @PostMapping("changeCurrentTenantId")
    @ApiOperation(value = "切换当前租户，并返回新的 token ")
    public WebResult changeCurrentTenantId(@RequestParam String newTenantId) {
        String userId = SecurityUtils.getUserContent().getId();
        service.changeCurrentTenant(newTenantId);
        String token = service.createToken(service.markUserContext(userId, newTenantId, null));
        System.out.println(token);
        return WebUtils.success(token);
    }

    @PostMapping("bindOpenId")
    @ApiOperation(value = "不再使用")
    public WebResult bindOpenId(@RequestParam String openId, @RequestParam String phone, @RequestParam(value = "code", required = false) String code) {
        service.bindOpenId(openId, phone, code);
        return WebUtils.success();
    }

    @PostMapping("bindStatus")
    @ApiOperation(value = "不再使用")
    public WebResult bindStatus(@RequestParam String openId) {
        return WebUtils.success(service.bindStatus(openId));
    }

    @PostMapping("register")
    @ApiOperation(value = "注册用户", notes = "新增一个用户")
    public WebResult save(@RequestBody UserAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("createOpenApiToken")
    @ApiOperation(value = "createOpenApiToken")
    public WebResult createOpenApiToken(@RequestParam String userName, @RequestParam String cnName) {
        return WebUtils.success(service.createOpenApiToken(userName, cnName));
    }

    @PostMapping("resetOpenApiToken")
    @ApiOperation(value = "createOpenApiToken")
    public WebResult resetOpenApiToken(@RequestParam String userID) {
        return WebUtils.success(service.createOpenApiToken(userID));
    }


    @PostMapping("unRegister/{id}")
    @ApiOperation(value = "注销用户", notes = "根据主键删除单个 User")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @GetMapping("/getCurrUserInfo")
    @ApiOperation(value = "获取当前用户信息")
    @ResponseBody
    public WebResult getInfo() {
        return WebUtils.success(SecurityUtils.getUserContent());
    }

    @PostMapping("changePwd")
    @ApiOperation(value = "修改密码")
    public WebResult changePwd(@RequestParam String oldPwd, @RequestParam String newPwd) {
        service.changePwd(oldPwd, newPwd, SecurityUtils.getUserContent().getId());
        return WebUtils.success();
    }

    @PostMapping("setUserToRole")
    @ApiOperation(value = "设置用户角色")
    public WebResult setRole(@RequestParam String userId, @RequestBody List<String> roleIds) {
        service.setRole(userId, roleIds);
        return WebUtils.success();
    }

    @GetMapping("listMyPower")
    @ApiOperation(value = "当前权限列表", notes = "查询当前用户的权限列表")
    public WebResult listMyPower() {
        return WebUtils.success(service.queryPower());
    }

    @GetMapping("listMyRole")
    @ApiOperation(value = "当前角色列表", notes = "查询当前用户的角色")
    public WebResult listMyRole() {
        return WebUtils.success(service.queryRoleByUserId(SecurityUtils.getUserContent().getId()));
    }

    @GetMapping("listMyApp")
    @ApiOperation(value = "当前应用列表", notes = "查询当前用户的应用")
    public WebResult listMyApp() {
        return WebUtils.success(service.queryAppByUserId(SecurityUtils.getUserContent().getId()));
    }

    @GetMapping("listMyTenant")
    @ApiOperation(value = "当前租户列表", notes = "查询当前用户的所有租户")
    public WebResult listMyTenant() {
        return WebUtils.success(service.queryAppByUserId(SecurityUtils.getUserContent().getId()));
    }

    @GetMapping("listApiRequest")
    @ApiOperation(value = "指定企业的 Api_Request 用户", notes = "")
    public WebResult<List<UserDto>> listApiRequest(@RequestParam(required = false) String entId) {
        return WebUtils.success(service.queryApiRequestUser(entId));
    }

    @PostMapping("sendRegisterCode")
    @ApiOperation(value = "发送注册验证码", notes = "发送注册验证码")
    public WebResult sendRegisterCode(@RequestParam String phone, @RequestParam(required = false) String tenantId) {
        service.sendRegisterCode(phone, tenantId);
        return WebUtils.success();
    }

}